//
//  LicensedOrderVC.m
//  Doctors
//
//  Created by 王森 on 16/4/13.
//  Copyright © 2016年 王森. All rights reserved.
//

#import "LicensedOrderVC.h"
#import "LXDSegmentControl.h"
#import "MJRefresh.h"
#import "PatientTableViewCell.h"
#import "LicensedOrderModel.h"
@interface LicensedOrderVC ()<LXDSegmentControlDelegate>
//{
//   __weak int selectIndex;
//}

@property(nonatomic,assign)int selectIndex;

@end


@implementation LicensedOrderVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTextTitleViewWithFrame:CGRectMake(0, 0, 60, 20) title:@"订单" fontSize:18];

    CGRect frame = CGRectMake(0,64, SCREEN_WIDTH, 48);
    NSArray * items = @[@"消费记录", @"开险记录"];
    
    LXDSegmentControlConfiguration * slide = [LXDSegmentControlConfiguration configurationWithControlType: LXDSegmentControlTypeSlideBlock items: items];
    slide.itemTextColor=UIColorFromRGB(0x909090);
    slide.itemSelectedTextColor=UIColorFromRGB(0x53cac3);
    slide.itemTextFont=[UIFont systemFontOfSize:14];
    
    slide.slideBlockColor=UIColorFromRGB(0x53cac3);
    LXDSegmentControl * slideControl = [LXDSegmentControl segmentControlWithFrame: frame configuration: slide delegate: self];
    slideControl.backgroundColor=[UIColor whiteColor];
    [self.view addSubview: slideControl];
    
//    [self initWithTableViewNib:@"PatientTableViewCell"];
    
    [self initWithTableViewNib:@"PatientTableViewCell" didSelectCellBlock:^(NSIndexPath *indexPath) {
        
    }];
    
    self.tableview.frame=CGRectMake(0, slideControl.bottom, SCREEN_WIDTH, ScreenHeight-64-48-48);
    
    __unsafe_unretained __typeof(self) weakSelf = self;
   
    
    
    [self setFooterfresh:^{
        weakSelf.page++;
         [weakSelf requestData:[NSString stringWithFormat:@"%d",weakSelf.selectIndex]];
        

}];
    
    [self setTableHeaderRefresh:^{
        [weakSelf.dataArray removeAllObjects];
        
        weakSelf.page=1;
        
        [weakSelf requestData:[NSString stringWithFormat:@"%d",weakSelf.selectIndex]];

    }];
    
    
    

    // Do any additional setup after loading the view from its nib.
}

- (void)segmentControl: (LXDSegmentControl *)segmentControl didSelectAtIndex: (NSUInteger)index
{
    [self.dataArray removeAllObjects];
    
    self.page=1;
    
    if (index==0) {
        _selectIndex=10;
        
        [self requestData:@"1"];
        
    }
    if (index==1) {
        _selectIndex=20;
        [self requestData:@"3"];
        
    }
    }
-(void)requestData:(NSString *)str{
    
    NSDictionary *dic=nil;
    
    dic=@{@"artisanId":[self getArtisanId],@"page":[NSString stringWithFormat:@"%d",self.page],@"size":@"15"};

    NSString *url=nil;
    
    if (_selectIndex==10) {
        url=@"/order/getUserOrderList";
        
    }
    else{
        url=@"/order/getOpenOrderList";
  
    }
    
    [self requestApiWithUrl:url dictionary:dic isCache:NO  success:^(id responseObject) {
        NSLog(@"%@",responseObject);
        
        NSArray *array=responseObject[@"orderList"];
        if (array.count==0) {
            self.page--;
            
        }
        for (NSDictionary *dic in array) {
            
            LicensedOrderModel *model=[[LicensedOrderModel alloc]initWithDic:dic];
            [self.dataArray addObject:model];
            
        }
        
        [self.tableview reloadData];
        
        
        
    } faild:^(NSError *error) {
        self.page--;

    }];
    
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//        __weak typeof(self)weakSelf=self;
    LicensedOrderModel *model;
    
    if (self.dataArray.count!=0) {
      model =self.dataArray[indexPath.row];

    }
    
    PatientTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PatientTableViewCell" forIndexPath:indexPath];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    cell.jianTouImg.hidden=YES;
   
    
    if (self.dataArray.count!=0) {
        model =self.dataArray[indexPath.row];
        [cell fuzhi:model];
    }
    
    
    
    //    [cell cellFuzhi:self.dataArray[indexPath.row]];
    
    //    __weak typeof(self)weakSef=self;
    
    
    
    return cell;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//    return 10;
    
    return self.dataArray.count;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 123;
    
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    NSLog(@"请重写方法");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
